<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>lsqrsolve</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : 11/01/2005</div>
    <p>
      <b>lsqrsolve</b> -  minimize the sum of the squares of
nonlinear functions, levenberg-marquardt algorithm</p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>[x [,v [,info]]]=lsqrsolve(x0,fct,m [,stop [,diag]])</tt>
      </dd>
      <dd>
        <tt>[x [,v [,info]]]=lsqrsolve(x0,fct,m ,fjac [,stop [,diag]])</tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>x0</b>
        </tt>: real vector (initial value of functions argument).</li>
      <li>
        <tt>
          <b>fct</b>
        </tt>: external (i.e function or list or string).</li>
      <li>
        <tt>
          <b>m</b>
        </tt>: integer, the number of functions.</li>
      <li>
        <tt>
          <b>fjac</b>
        </tt>: external (i.e function or list or string).</li>
      <li>
        <tt>
          <b>stop</b>
        </tt>: optional vector
          <tt>
          <b>[ftol,xtol,gtol,maxfev,epsfcn,factor]</b>
        </tt> the
          default value is <tt>
          <b>[1.d-8,1.d-8,1.d-5,1000,0,100]</b>
        </tt>
        <ul>
          <li>
            <tt>
              <b>ftol</b>
            </tt>: A positive real number,termination occurs when both
                  the actual and predicted relative reductions in the
                  sum of squares are at most ftol.  therefore, ftol
                  measures the relative error desired in the sum of
                  squares.</li>
          <li>
            <tt>
              <b>xtol</b>
            </tt>: A positive real number, termination occurs when the
             relative error between two consecutive iterates is at
             most xtol. therefore, xtol measures the relative error
             desired in the approximate solution.</li>
          <li>
            <tt>
              <b>gtol</b>
            </tt>: A nonnegative input variable. termination
		  occurs when the cosine of the angle between fct(x) and
		  any column of the jacobian is at most gtol in absolute
		  value. therefore, gtol measures the orthogonality
		  desired between the function vector and the columns
		  of the jacobian.</li>
          <li>
            <tt>
              <b>maxfev</b>
            </tt>: A positive integer, termination occurs when the
             number of calls to fct is at least maxfev by the end of
             an iteration.</li>
          <li>
            <tt>
              <b>epsfcn</b>
            </tt>: A positive real number, used in determining a
             suitable step length for the forward-difference
             approximation. this approximation assumes that the
             relative errors in the functions are of the order of
             epsfcn. if epsfcn is less than the machine precision, it
             is assumed that the relative errors in the functions are
             of the order of the machine precision.</li>
          <li>
            <tt>
              <b>factor</b>
            </tt>: A positive real number, used in determining the
            initial step bound. this bound is set to the product of
            factor and the euclidean norm of diag*x if nonzero, or
            else to factor itself. in most cases factor should lie in
            the interval (.1,100.). 100. is a generally recommended
            value.</li>
        </ul>
      </li>
      <li>
        <tt>
          <b>diag</b>
        </tt>: is an array of length n.  diag
         must contain positive entries that serve as
         multiplicative scale factors for the variables.</li>
      <li>
        <tt>
          <b>x :  </b>
        </tt>real vector (final value of function argument, estimated zero).</li>
      <li>
        <tt>
          <b>v :  </b>
        </tt>real vector (value of functions at x).</li>
      <li>
        <tt>
          <b>info</b>
        </tt>:  termination indicator<ul>
          <li>
            <tt>
              <b>0</b>
            </tt>: improper input parameters.</li>
          <li>
            <tt>
              <b>1</b>
            </tt>: algorithm estimates that the relative error between x and the solution  is at most tol.</li>
          <li>
            <tt>
              <b>2</b>
            </tt>: number of calls to fcn reached</li>
          <li>
            <tt>
              <b>3</b>
            </tt>: tol is too small. No further improvement in the approximate solution  x is possible.</li>
          <li>
            <tt>
              <b>4</b>
            </tt>: iteration is not making good progress.</li>
        </ul>
      </li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
    minimize the sum of the squares of m nonlinear functions in n
     variables by a modification of the levenberg-marquardt
     algorithm. the user must provide a subroutine which calculates
     the functions. the jacobian is then calculated by a
     forward-difference approximation.</p>
    <p>
     minimize <tt>
        <b>sum(fct(x,m).^2)</b>
      </tt> where
    <tt>
        <b>fct</b>
      </tt> is function from <tt>
        <b>R^n</b>
      </tt> to <tt>
        <b>R^m</b>
      </tt>
    </p>
    <p>
      <tt>
        <b>fct</b>
      </tt> should be :
    </p>
    <dl>
      <dd>
        <b></b>a Scilab function whose calling sequence is <tt>
          <b>v=fct(x,m)</b>
        </tt> given <tt>
          <b>x</b>
        </tt> and
    <tt>
          <b>m</b>
        </tt>.</dd>
      <dd>
        <b></b>a character string which  refers to a C or Fortran routine  which must be linked to Scilab. <p>
    Fortran calling sequence should be
   <tt>
            <b>fct(m,n,x,v,iflag)</b>
          </tt>  where <tt>
            <b>m</b>
          </tt>,
    <tt>
            <b>n</b>
          </tt>, <tt>
            <b>iflag</b>
          </tt> are integers, <tt>
            <b>x</b>
          </tt> a
    double precision vector of size  <tt>
            <b>n</b>
          </tt> and <tt>
            <b>v</b>
          </tt> a
    double precision vector of size  <tt>
            <b>m</b>
          </tt>.
    </p>
        <p>  
    C calling sequence should be
    <tt>
            <b>fct(int *m, int *n, double x[],double v[],int *iflag)</b>
          </tt>
        </p>
      </dd>
    </dl>
    <p>
      <tt>
        <b>fjac</b>
      </tt> is an external which returns <tt>
        <b>v=d(fct)/dx (x)</b>
      </tt>.
     it should be :</p>
    <dl>
      <dd>
        <b>a Scilab function</b>whose calling
    sequence is <tt>
          <b>J=fjac(x,m)</b>
        </tt> given <tt>
          <b>x</b>
        </tt> and
    <tt>
          <b>m</b>
        </tt>.</dd>
      <dd>
        <b>a character string</b>it refers to a C or Fortran routine
    which must be linked to Scilab. <p>
    Fortran calling sequence should be
   <tt>
            <b>fjac(m,n,x,jac,iflag)</b>
          </tt>  where <tt>
            <b>m</b>
          </tt>,
    <tt>
            <b>n</b>
          </tt>, <tt>
            <b>iflag</b>
          </tt> are integers, <tt>
            <b>x</b>
          </tt> a
    double precision vector of size  <tt>
            <b>n</b>
          </tt> and <tt>
            <b>jac</b>
          </tt> a
    double precision vector of size  <tt>
            <b>m*n</b>
          </tt>.
    </p>
        <p>  
    C calling sequence should be
    <tt>
            <b>fjac(int *m, int *n, double x[],double v[],int *iflag)</b>
          </tt>
        </p>
      </dd>
    </dl>
    <p> return -1 in iflag to stop the algoritm if the function
   or jacobian could not be evaluated.</p>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>

// A simple example with lsqrsolve 
a=[1,7;
   2,8
   4 3];
b=[10;11;-1];
function y=f1(x,m),y=a*x+b;endfunction
[xsol,v]=lsqrsolve([100;100],f1,3)
xsol+a\b


function y=fj1(x,m),y=a;endfunction
[xsol,v]=lsqrsolve([100;100],f1,3,fj1)
xsol+a\b

// Data fitting problem
// 1 build the data
a=34;b=12;c=14;
deff('y=FF(x)','y=a*(x-b)+c*x.*x');
X=(0:.1:3)';Y=FF(X)+100*(rand()-.5);

//solve
function e=f1(abc,m)
  a=abc(1);b=abc(2),c=abc(3),
  e=Y-(a*(X-b)+c*X.*X);
endfunction
[abc,v]=lsqrsolve([10;10;10],f1,size(X,1));
abc
norm(v)

 
  </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="../programming/external.htm">
        <tt>
          <b>external</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="quapro.htm">
        <tt>
          <b>quapro</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="linpro.htm">
        <tt>
          <b>linpro</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="optim.htm">
        <tt>
          <b>optim</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="fsolve.htm">
        <tt>
          <b>fsolve</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
    <h3>
      <font color="blue">Used Function</font>
    </h3>lmdif, lmder from  minpack, Argonne National Laboratory.</body>
</html>
